<template>
  <div>
    <el-form :model="dataForm" ref="dataForm" label-width="100px" size="small" :rules="rules">
      <el-form-item label="字段名称:" prop="dictName">
        <el-input v-model="dataForm.dictName" :disabled="!!(flag==='det')" placeholder="请输入字段名称" maxlength="15" clearable></el-input>
      </el-form-item>
      <el-form-item label="代码:" prop="dictNo">
        <el-input v-model="dataForm.dictNo" placeholder="请输入代码" disabled maxlength="10" clearable></el-input>
      </el-form-item>
      <el-form-item label="字段描述:" prop="remark">
        <el-input v-model="dataForm.remark" type="textarea" placeholder="请输入字段描述" :disabled="!!(flag==='det')" maxlength="30" show-word-limit clearable></el-input>
      </el-form-item>
    </el-form>
    <el-row style="margin-top:10px">
      <!-- <el-table :data="childDictList.slice((dictData.pageNum-1)*dictData.pageSize,dictData.pageNum*dictData.pageSize)" tooltip-effect="light" class="tc-table" size="mini"> -->
      <el-table :data="childDictList" tooltip-effect="light" class="tc-table" size="mini">
        <template v-for="(column, index) in DICT_CHILDREN_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column prop="operation" class-name="oper-col" label="操作" v-if="flag!=='det'" :width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="editRow(scope.row, scope.$index)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="deleteRow(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="addButton" @click="addDictChild" v-if="flag!=='det'">
        <i class="fa fa-plus" /> 添加
      </div>
    </el-row>
    <!-- <el-row class="table-top">
      <pagination :pageNum.sync="dictData.pageNum" :curSizes.sync="dictData.pageSize" :pageSizes="[10]" :total="childDictList.length" />
    </el-row> -->
    <el-row style="margin-top:5px;text-align:center" v-if="flag !== 'det'">
      <el-button type="" size="small" @click="close">
        取消
      </el-button>
      <el-button type="primary" size="small" @click="saveDict">
        <fa-icon class="fa-check" />保存</el-button>
    </el-row>
    <el-row style="margin-top:5px;text-align:center" v-if="flag === 'det'">
      <el-button type="" size="small" @click="close">
        取消
      </el-button>
    </el-row>
    <el-dialog :title="editData.id? '编辑':'新增'" :visible.sync="dictDialog" width="20%" append-to-body :close-on-click-modal="false">
      <dict-child v-if="dictDialog" :parentId="dictId" :editData="editData" @uppdataSuccess="uppdataSuccess" @close="closeChild" />
    </el-dialog>
  </div>
</template>

<script>
import { DICT_CHILDREN_HEAD_LIST } from '../js/contant.js'
import DictChild from './DictChild'
import { CONSTANT_OBJ } from '@/utils/constant'
import api from '../api'
export default {
  components: { DictChild },
  props: {
    dictId: String,
    flag: String
  },
  data () {
    return {
      DICT_CHILDREN_HEAD_LIST,
      dictDialog: false,
      editData: {},
      dataForm: {
        id: '',
        dictName: '',
        dictNo: '',
        remark: ''
      },
      currentIndex: -1,
      dictData: {
        pageNum: 1,
        pageSize: 10
      },
      childDictList: [],
      rules: {
        dictName: [{ required: true, message: '请输入字段名称', trigger: 'blur' }],
        remark: [{ required: true, message: '请输入字段描述', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    // 获取详细数据
    getData () {
      api.queryById(this.dictId).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataForm = data
          this.childDictList = (data && data.childDictList) || []
        }
      })
    },
    // 保存
    saveDict () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          api.postEdit(this.dataForm).then(res => {
            const { responseCode } = res
            if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
              this.$emit('uppdataSuccess')
            }
          })
        }
      })
    },
    // 编辑
    editRow (row, index) {
      this.currentIndex = index
      this.editData = { ...row, sort: index }
      this.dictDialog = true
    },
    // 删除
    deleteRow (row, index) {
      api.deleteDict(row.id).then(res => {
        const { responseCode } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.getData(0)
          this.childDictList.splice(index, 1)
        }
      })
    },
    // 添加子字典
    addDictChild () {
      this.currentIndex = -1
      this.editData = {sort: this.childDictList.length}
      this.dictDialog = true
    },
    // 关闭弹框
    uppdataSuccess (data) {
      const { currentIndex } = this
      if (currentIndex !== -1) {
        if (this.childDictList[currentIndex].sort === undefined) {
          data.sort = currentIndex
        }
        this.childDictList[currentIndex] = { ...this.childDictList[currentIndex], ...data }
      } else {
        data.sort = this.childDictList.length
        this.childDictList.push(data)
      }
      this.dictDialog = false
      this.getData()
    },
    close () {
      this.$emit('close')
    },
    closeChild () {
      this.dictDialog = false
    }
  }
}
</script>

<style lang="postcss" scoped>
.addButton {
  box-sizing: border-box;
  width: 100%;
  text-align: center;
  padding: 6px 0;
  border: 1px solid $main-border-color;
  border-top: none;
  color: #67c23a;
  cursor: pointer;
  margin: 0 auto;
}
.table-top {
  margin-top: 5px;
  text-align: right;
}
</style>
